<!DOCTYPE html>
<html>
	<head>
		<meta charset="utf-8">
		<title></title>
	</head>
	<body>
		<table id="shared_file_dg">
		</table>
		<div id="upload" class="easyui-dialog" style="padding:20px 6px;width:80%;" data-options="inline:true,modal:true,closed:true,title:'Login'">
			<tr>
				<td>
					<form id="shared_file_dialog_form" enctype="multipart/form-data">
						<input id="file" name="file" style="width:270px" type="file">
					</form>
				</td>
			</tr>
			<tr>
				<td style="width:280px;" align="left">文件说明：</td>
			</tr>
			<tr>
				<td><textarea id="notes" name="notes" rows="11" cols="35">
					</textarea></td>
			</tr>
			<tr>
				<td align="center">
					<select id="public">
						<option value="0">保密</option>
						<option value="1">公开</option>
					</select>
				</td>
			</tr>
			<div class="dialog-button">
				<a href="javascript:void(0)" class="easyui-linkbutton" style="width:100%;height:35px" onclick="javascript:confirm()">Confirm</a>
			</div>
			<div class="dialog-button">
				<a href="javascript:void(0)" class="easyui-linkbutton" style="width:100%;height:35px" onclick="$('#upload').dialog('close')">Cancel</a>
			</div>
		</div>
		<script type="text/javascript">
			$(function() {
				setPublic();
				$('#shared_file_dg').datagrid({
					url: 'files_page',
					scrollbarSize: 0,
					method: "get",
					pagination: true,
					fit: true,
					fitColumns: true,
					border: false,
					pageSize: 10,
					columns: [
						[{
								field: 'fileName',
								title: 'fileName',
								editor: {type:'text'},
								width: 132
							},
							{
								field: 'fileType',
								title: 'fileType',
								width: 68
							},
							{
								field: 'fileSize',
								title: 'fileSize',
								width: 50
							},
							{
								field: 'provider',
								title: 'provider',
								width: 52,
								formatter: function(value) {
									return value.username;
								}
							},
							{
								field: 'state',
								title: 'public',
								width: 38,
								formatter: function(value) {
									if (value == 1) {
										return '公开';
									} else {
										return '保密';
									}
								}
							},
						]
					]
				});
				$("#public option[value='1']").attr("selected", "selected");
			})
			function add(){
				$("#shared_file_dialog_form").form("clear");
				$("#notes").val("");
				$('#upload').dialog('open').dialog('center');
			}
			
			function deleted(){
				var rows = $("#shared_file_dg").datagrid("getSelections");
				if (rows.length < 1) {
					$.messager.alert("信息提示", "请勾选您要删除的数据！", "info");
				} else {
					$.messager.confirm("信息提示", "确定要删除选中的记录吗？", function(result) {
						if (result) {
							var ids = [];
							$(rows).each(function() {
								ids.push(this.id);
							});
							$.ajax({
								url: "/filesharingsystem/files",
								method: "delete",
								contentType: "application/json;charset=utf-8",
								data: JSON.stringify(ids),
								success: function(data) { //"ok","error"
									if (data == "ok") {
									shared_file_reload();
									} else {
										$.messager.alert("信息提示", "删除失败！", "info");
									}
								}
							});
						}
					});
				}
			}
			
			function put(){
				var rows = $("#shared_file_dg").datagrid("getSelections");
				
				if (rows.length == 0) {
					$.messager.alert("信息提示", "请勾选您要修改的数据！", "info");
				} else {
					$.messager.confirm("信息提示", "确定要修改选中的文件吗？", function(result) {
						if (result) {
							var ids = [];
							$(rows).each(function() {
								ids.push(this.id);
							});
							$.ajax({
								url: "/filesharingsystem/files",
								method: "put",
								contentType: "application/json;charset=utf-8",
								data: JSON.stringify(ids),
								success: function(data) { //"ok","error"
									if (data == "ok") {
										shared_file_reload();
									} else {
										$.messager.alert("信息提示", "修改失败！", "info");
									}
								}
							});
						}
					});
				
				}
			}
			
			function select(){
				var rows = $("#shared_file_dg").datagrid("getSelections");
				if (rows.length < 1) {
					$.messager.alert("信息提示", "请勾选您要下载的文件！", "info");
				} else {
					$.messager.confirm("信息提示", "确定要下载选中的文件吗？", function(result) {
						if (result) {
							var ids = [];
							var fileNames = [];
							$(rows).each(function() {
								ids.push(this.id);
								fileNames.push(this.fileName);
							});
				
							for (var i = 0; i < ids.length; i++) {								
								download(fileNames[i],ids[i]);
							}
						}
					});
				}
			}
			function confirm(){
				var formdata = new FormData($("#shared_file_dialog_form")[0]);
				formdata.append('notes', $("#notes").val());
				formdata.append('state', $("#public option:selected").val());
				$.ajax({
					url: "/filesharingsystem/file",
					method:'post',
					data: formdata,
					cache: false,
					contentType: false,
					processData: false,
					success: function(data) { //"ok","error"
						$("#shared_file_dialog").dialog("close");
						shared_file_reload();	
					},
					error:function(){
						$.messager.alert("信息提示", "提交失败！", "info");
					}
				});
			}

			function shared_file_reload() {
				$('#upload').dialog('close');
				$("#shared_file_dg").datagrid("reload");
			}
			
			function setPublic(){
				var str = $("#info3").text();
				var index = str.indexOf(":");
				var ti = str.slice(index+1,str.length);
				if(ti=="guest"){
					$("#public").hide();
				}
			}
			function download(fileName,id){
				var a = document.createElement("a");
				a.download = fileName;
				a.href = "/filesharingsystem/files?id="+id;
				a.click();	
			}
		</script>
	</body>
</html>
